package com.pingan.agentbank.aaos.service;

import com.pingan.agentbank.aaos.pojo.ApproFileInfo;
import com.pingan.agentbank.aaos.pojo.Config;
import com.pingan.vframe.lang.Inject;
import com.pingan.vframe.logging.Log;
import com.pingan.vframe.logging.LogFactory;
import com.pingan.vframe.util.JaDateTime;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class CleanService {

    private final Log log = LogFactory.getLog();

    @Inject
    ApproFileDaoService approFileDaoService;
    @Inject
    ApproLineDaoService approLineDaoService;



    public void BackUpClean(Config config){

        long cleanTime = config.getBackUpOverDue()*24*60*60*1000L;
        long overDueTime = System.currentTimeMillis() - cleanTime;
        JaDateTime time = JaDateTime.valueOf(overDueTime);
        //获得清理时间字符串
        String timeStr = time.getYearString()+time.getMonthString()+time.getDayString();
        time = JaDateTime.valueOf(timeStr);


        log.info("[ AAOS ] "+config.getBank()+" (CleanModule-bk) "+"check overdue backup file");
        String[] strs = new File(config.getBackDir()).list();
        if (strs.length==0){
            log.info("[ AAOS ] "+config.getBank()+" (CleanModule-bk) "+"no overdue backup file detected，task over");
            return;
        }

        //找到文件夹中所有.ok文件放入list
        List<String> strList = new ArrayList<>();
        for (String str:strs){
            if (str.matches(".{20,40}\\.ok")){
                strList.add(str);
            }
        }

        if (strList.size() == 0){
            log.info("[ AAOS ] "+config.getBank()+" (CleanModule-bk) "+"no overdue backup file detected，task over");
            return;
        }
        int count = 0;
        for (String str:strList){

            //获取时间
            String date = str.substring(0,str.lastIndexOf("-"));
            date = date.substring(date.lastIndexOf("-")+1);
            JaDateTime jaDateTime = JaDateTime.valueOf(date);
            //过期则进行删除
            if (JaDateTime.isAfter(time,jaDateTime)){
                String overDueOk = config.getBackDir()+"/"+str;
                String overDue = overDueOk.substring(0,overDueOk.lastIndexOf("."))+".txt";

                log.info("[ AAOS ] "+config.getBank()+" (CleanModule-bk) "+"clean overdue backup file"+overDue);
                new File(overDueOk).delete();
                new File(overDue).delete();
                count++;
            }
        }
        if (count == 0){
            log.info("[ AAOS ] "+config.getBank()+" (CleanModule-bk) "+"no overdue backup file detected，task over");
        } else {
            log.info("[ AAOS ] "+config.getBank()+" (CleanModule-bk) "+"clean backup file, task over");
        }

    }

    public void DbClean(Config config){

        //获得清理时间(当前时间 - 过期时间)
        long cleanTime = config.getDbOverDue()*24*60*60*1000L;
        long overDueTime = System.currentTimeMillis() - cleanTime;
        JaDateTime time = JaDateTime.valueOf(overDueTime);
        //获得清理时间字符串
        String timeStr = time.getYearString()+time.getMonthString()+time.getDayString()+" "+time.getHourString()+time.getMinuteString()+time.getSecondString();
        time = JaDateTime.valueOf(timeStr);

        //获取数据库中过期的ApproFileInfo
        log.info("[ AAOS ] "+config.getBank()+" (CleanModule-db) "+"check overdue record");
        List<ApproFileInfo> list = approFileDaoService.queryOverDue(time);

        if (list.size() == 0){
            log.info("[ AAOS ] "+config.getBank()+" (CleanModule-db) "+"no overdue record detected，task over");
        } else {
            //逐个删除  先删除line 再删除file
            for (ApproFileInfo approFileInfo:list){
                log.info("[ AAOS ] "+config.getBank()+" (CleanModule-db) "+"clean overdue record："+approFileInfo.getFilename());
                //在DBTask中 不用单独提交事务
                //删除line
                approLineDaoService.deleteByFileId(approFileInfo.getId());
                //删除file
                approFileDaoService.deleteOneApproFileInfo((int) approFileInfo.getId());
            }
            log.info("[ AAOS ] "+config.getBank()+" (CleanModule-db) "+"clean overdue record,task over");
        }
    }
}
